"use strict";
cc._RF.push(module, '59b0ah3xdJKEZ5WDKeHb1h0', 'SteeringCtrl');
// cases/SteeringCtrl.ts

"use strict";
// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var GameWorld_1 = require("./entity/GameWorld");
var Obstacle_1 = require("./entity/Obstacle");
var Vehicle_1 = require("./entity/Vehicle");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var offsetPursuit = 50;
var SteeringCtrl = /** @class */ (function (_super) {
    __extends(SteeringCtrl, _super);
    function SteeringCtrl() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.vehiclePrefab = null;
        _this.touchNode = null;
        _this.gameNode = null;
        _this.pen = null;
        _this.flowPen = null;
        _this.obstaclePrefab = null;
        _this.desc = null;
        _this._curTestIndex = 0;
        _this.testList = [
            {
                func: _this.arriveTest,
                desc: "到达某个点,点击屏幕将到达对应位置停下"
            },
            {
                func: _this.seekTest,
                desc: "寻找,点击屏幕将不断寻找到对应位置"
            },
            {
                func: _this.fleeTest,
                desc: "距离目标特定距离后开始远离某个目标点，将远离点击屏幕的位置"
            },
            {
                func: _this.wanderTest,
                desc: "徘徊"
            },
            {
                func: _this.pursuitTest,
                desc: "追逐目标，根据目标速度预测最终寻找的位置"
            },
            {
                func: _this.evadeTest,
                desc: "躲避目标，跟追逐相反"
            },
            {
                func: _this.interposeTest,
                desc: "移动到两个目标中心"
            },
            {
                func: _this.obstacleAvoidanceTest,
                desc: "躲避圆形障碍物"
            },
            {
                func: _this.followPathTest,
                desc: "简单的路径跟随"
            },
            {
                func: _this.offsetPursuitTest,
                desc: "偏移追逐，设定追逐目标偏移，实现战斗队列等移动行为"
            },
            {
                func: _this.flockingTest,
                desc: "群集，分散，聚拢，队列（对齐方向）用于简单模拟鱼群，蚁群等群体运动之间的关系"
            },
            {
                func: _this.flowTest,
                desc: "根据流场方向移动"
            },
        ];
        _this._targetPos = cc.v2();
        _this._world = null;
        _this._testVehicle = null;
        _this._pursuitVehicle = null;
        _this._PosList = [
            // [0, -offsetpet],
            [-offsetPursuit / 2, -offsetPursuit * 1], [offsetPursuit / 2, -offsetPursuit * 1],
            [-offsetPursuit, -offsetPursuit * 2], [0, -offsetPursuit * 2], [offsetPursuit, -offsetPursuit * 2],
            [-offsetPursuit * 3 / 2, -offsetPursuit * 3], [-offsetPursuit / 2, -offsetPursuit * 3], [offsetPursuit / 2, -offsetPursuit * 3], [offsetPursuit * 3 / 2, -offsetPursuit * 3]
        ];
        return _this;
    }
    SteeringCtrl.prototype.start = function () {
        this._registerTouch();
        this.pen.fillColor = cc.Color.RED;
        this._createWorld();
        this._curTestIndex = 0;
        this.reset();
    };
    SteeringCtrl.prototype._registerTouch = function () {
        this.touchNode.on(cc.Node.EventType.TOUCH_START, this._handleTouchStart, this);
        this.touchNode.on(cc.Node.EventType.TOUCH_MOVE, this._handleTouchMove, this);
        this.touchNode.on(cc.Node.EventType.TOUCH_END, this._handleTouchEnd, this);
        this.touchNode.on(cc.Node.EventType.TOUCH_CANCEL, this._handleTouchEnd, this);
    };
    SteeringCtrl.prototype._handleTouchStart = function (event) {
    };
    SteeringCtrl.prototype._handleTouchMove = function (event) {
    };
    SteeringCtrl.prototype._handleTouchEnd = function (event) {
        var delta = event.getLocation();
        var vec = this.gameNode.convertToNodeSpaceAR(delta);
        cc.Vec2.set(this._targetPos, vec.x, vec.y);
        if (this._testVehicle)
            this._testVehicle.steering.setTarget(vec);
        if (this._world && this._testVehicle && this._testVehicle.steering.isFlowOn()) {
            var rowandcol = this._world.getColAndRowByPosition(vec.x, vec.y);
            this._world.generateFlowField(rowandcol[0], rowandcol[1]);
            this.flowPen.clear();
            this._world.render(this.flowPen);
            for (var i = 0, len = this._world.agents.length; i < len; i++) {
                this._world.agents[i].steering.setTarget(this._targetPos);
            }
        }
    };
    SteeringCtrl.prototype._createWorld = function () {
        this._world = new GameWorld_1.default(-cc.winSize.width * 0.5, -cc.winSize.height * 0.5, cc.winSize.width, cc.winSize.height);
    };
    //到达
    SteeringCtrl.prototype.arriveTest = function () {
        this.gameNode.destroyAllChildren();
        var vehicle = this.createVehicle();
        this._testVehicle = vehicle;
        this._testVehicle.steering.arriveOn();
        this._targetPos.set(cc.Vec2.ZERO);
        this._testVehicle.steering.setTarget(this._targetPos);
    };
    //寻找
    SteeringCtrl.prototype.seekTest = function () {
        this.gameNode.destroyAllChildren();
        var vehicle = this.createVehicle();
        this._testVehicle = vehicle;
        this._testVehicle.steering.seekOn();
        this._targetPos.set(cc.Vec2.ZERO);
        this._testVehicle.steering.setTarget(this._targetPos);
    };
    //远离
    SteeringCtrl.prototype.fleeTest = function () {
        this.gameNode.destroyAllChildren();
        var vehicle = this.createVehicle();
        this._testVehicle = vehicle;
        this._testVehicle.steering.fleeOn();
        this._targetPos.set(cc.Vec2.ZERO);
        this._testVehicle.steering.setTarget(this._targetPos);
    };
    //徘徊
    SteeringCtrl.prototype.wanderTest = function () {
        this.gameNode.destroyAllChildren();
        var vehicle = this.createVehicle();
        this._testVehicle = vehicle;
        this._testVehicle.steering.wanderOn();
    };
    //追逐
    SteeringCtrl.prototype.pursuitTest = function () {
        this.gameNode.destroyAllChildren();
        var vehicle = this.createVehicle();
        this._testVehicle = vehicle;
        this._testVehicle.steering.arriveOn();
        this._targetPos.set(cc.Vec2.ZERO);
        this._testVehicle.steering.setTarget(this._targetPos);
        vehicle.maxSpeed = 180;
        var vehicle2 = this.createVehicle();
        vehicle2.node.setPosition(300, 300);
        vehicle2.maxSpeed = 150;
        vehicle2.steering.pursuitOn(vehicle);
        this._pursuitVehicle = vehicle2;
    };
    //躲避
    SteeringCtrl.prototype.evadeTest = function () {
        this.gameNode.destroyAllChildren();
        var vehicle = this.createVehicle();
        this._testVehicle = vehicle;
        this._testVehicle.steering.arriveOn();
        this._targetPos.set(cc.Vec2.ZERO);
        this._testVehicle.steering.setTarget(this._targetPos);
        // vehicle.maxSpeed = 180;
        var vehicle2 = this.createVehicle();
        vehicle2.node.setPosition(100, 100);
        // vehicle2.maxSpeed = 150;
        vehicle2.steering.evadeOn(vehicle);
        this._pursuitVehicle = vehicle2;
    };
    //插入
    SteeringCtrl.prototype.interposeTest = function () {
        this.gameNode.destroyAllChildren();
        var vehicle2 = this.createVehicle();
        vehicle2.node.setPosition(Math.random() * 300, Math.random() * (-300));
        var vehicle3 = this.createVehicle();
        vehicle3.node.setPosition(Math.random() * (-300), Math.random() * (300));
        vehicle2.steering.pursuitOn(vehicle3);
        vehicle3.steering.pursuitOn(vehicle2);
        this._testVehicle = null;
        var vehicle = this.createVehicle();
        vehicle.steering.interposeOn(vehicle2, vehicle3);
    };
    //躲避圆形碰撞体
    SteeringCtrl.prototype.obstacleAvoidanceTest = function () {
        this.gameNode.destroyAllChildren();
        var vehicle = this.createVehicle();
        this._testVehicle = vehicle;
        this._testVehicle.steering.arriveOn();
        this._testVehicle.steering.obstacleAvoidanceOn();
        this._targetPos.set(cc.Vec2.ZERO);
        this._testVehicle.steering.setTarget(this._targetPos);
        this._world.obstacles.length = 0;
        for (var i = 0; i < 5; i++) {
            var node = cc.instantiate(this.obstaclePrefab);
            node.parent = this.gameNode;
            node.setPosition(-Math.random() * cc.winSize.width + cc.winSize.width * 0.5, -Math.random() * cc.winSize.height + cc.winSize.height * 0.5);
            var ob = node.getComponent(Obstacle_1.default);
            ob.radius = Math.random() * 10 + 30;
            node.width = ob.radius * 2;
            node.height = ob.radius * 2;
            this._world.obstacles.push(ob);
        }
    };
    //简单路径跟随
    SteeringCtrl.prototype.followPathTest = function () {
        this.gameNode.destroyAllChildren();
        var vehicle = this.createVehicle();
        this._testVehicle = vehicle;
        this._testVehicle.steering.followPathOn();
    };
    //偏移追逐
    SteeringCtrl.prototype.offsetPursuitTest = function () {
        this.gameNode.destroyAllChildren();
        var vehicle = this.createVehicle();
        this._testVehicle = vehicle;
        this._testVehicle.steering.arriveOn();
        for (var i = 0, len = this._PosList.length; i < len; i++) {
            vehicle = this.createVehicle();
            var offset = this._PosList[i];
            vehicle.node.setPosition(this._testVehicle.position.x + offset[0], this._testVehicle.position.y + offset[1]);
            var dis = cc.v2(offset[0], offset[1]);
            vehicle.steering.offsetPursuitOn(this._testVehicle, dis);
        }
    };
    //集群
    SteeringCtrl.prototype.flockingTest = function () {
        this.gameNode.destroyAllChildren();
        this._world.agents.length = 0;
        for (var i = 0, len = 30; i < len; i++) {
            var vehicle = this.createVehicle();
            vehicle.node.setPosition(-Math.random() * cc.winSize.width + cc.winSize.width * 0.5, -Math.random() * cc.winSize.height + cc.winSize.height * 0.5);
            if (i == 0) {
                vehicle.maxSpeed = 70;
                vehicle.node.children[0].color = cc.Color.RED;
                vehicle.steering.wanderOn();
                this._testVehicle = vehicle;
                this._world.agents.push(vehicle);
            }
            else {
                vehicle.steering.flockingOn();
                vehicle.steering.wanderOn();
                vehicle.steering.evadeOn(this._testVehicle);
                this._world.agents.push(vehicle);
                vehicle.isTag = i % 2 == 0;
            }
        }
    };
    //流场
    SteeringCtrl.prototype.flowTest = function () {
        var rowandcol = this._world.getColAndRowByPosition(0, 0);
        this._world.generateFlowField(rowandcol[0], rowandcol[1]);
        this.flowPen.clear();
        this._world.render(this.flowPen);
        this.gameNode.removeAllChildren();
        this._world.agents.length = 0;
        for (var i = 0, len = 30; i < len; i++) {
            var vehicle = this.createVehicle();
            vehicle.node.setPosition(-Math.random() * cc.winSize.width + cc.winSize.width * 0.5, -Math.random() * cc.winSize.height + cc.winSize.height * 0.5);
            vehicle.steering.flowOn();
            vehicle.steering.arriveOn();
            // vehicle.steering.separationOn();
            this._targetPos.set(cc.Vec2.ZERO);
            vehicle.steering.setTarget(this._targetPos);
            vehicle.maxSpeed = 80;
            // this._world.agents.push(vehicle);
            // vehicle.isTag = i % 2 == 0;
            if (i == 0)
                this._testVehicle = vehicle;
        }
    };
    SteeringCtrl.prototype.reset = function () {
        var test = this.testList[this._curTestIndex];
        test.func.call(this);
        this.desc.string = test.desc;
    };
    SteeringCtrl.prototype.tonext = function () {
        if (this._curTestIndex++ >= this.testList.length) {
            this._curTestIndex = 0;
        }
        this.reset();
    };
    SteeringCtrl.prototype.createVehicle = function () {
        var node = cc.instantiate(this.vehiclePrefab);
        node.parent = this.gameNode;
        var vehicle = node.getComponent(Vehicle_1.default);
        vehicle.setWorld(this._world);
        vehicle.init();
        vehicle.steering.debugPen = this.pen;
        node.setPosition(0, 0);
        return vehicle;
    };
    SteeringCtrl.prototype.update = function (dt) {
        if (this._world.isDebug) {
            this.pen.clear();
        }
    };
    __decorate([
        property(cc.Prefab)
    ], SteeringCtrl.prototype, "vehiclePrefab", void 0);
    __decorate([
        property(cc.Node)
    ], SteeringCtrl.prototype, "touchNode", void 0);
    __decorate([
        property(cc.Node)
    ], SteeringCtrl.prototype, "gameNode", void 0);
    __decorate([
        property(cc.Graphics)
    ], SteeringCtrl.prototype, "pen", void 0);
    __decorate([
        property(cc.Graphics)
    ], SteeringCtrl.prototype, "flowPen", void 0);
    __decorate([
        property(cc.Prefab)
    ], SteeringCtrl.prototype, "obstaclePrefab", void 0);
    __decorate([
        property(cc.Label)
    ], SteeringCtrl.prototype, "desc", void 0);
    SteeringCtrl = __decorate([
        ccclass
    ], SteeringCtrl);
    return SteeringCtrl;
}(cc.Component));
exports.default = SteeringCtrl;

cc._RF.pop();